/*
 * 品智科技S32K144开发板
 * 01-基础工程
 * 建立一个常用的工程,包含数个常用函数
 *
 * IO配置
 * KEY1 PTC12
 * KEY2 PTC13
 * KEY3 PTB2
 * LED1 PTD16
 * LED2 PTD15
 * LED3 PTD1
 * LED4 PTD0
 */
#include "Cpu.h"
#include "delay.h"
#include "uart.h"
#include "key.h"
#include "oled.h"
#include "tim.h"
#include "string.h"
#include "app_init.h"
#include "can.h"

volatile int exit_code = 0;

#define LED1(x) PINS_DRV_WritePin(PTD, 16, !x);
#define LED2(x) PINS_DRV_WritePin(PTD, 15, !x);
#define LED3(x) PINS_DRV_WritePin(PTD, 1, !x);
#define LED4(x) PINS_DRV_WritePin(PTD, 0, !x);

#define LED1Toggle() PINS_DRV_TogglePins(PTD, (1 << 16))
#define LED2Toggle() PINS_DRV_TogglePins(PTD, (1 << 15))
#define LED3Toggle() PINS_DRV_TogglePins(PTD, (1 << 1))
#define LED4Toggle() PINS_DRV_TogglePins(PTD, (1 << 0))

int main(void) {
    float adcVolt1 = 0.0f;
    float adcVolt2 = 0.0f;
    /* Write your local variable definition here */
    int MCU_Freq;
    /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
    PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
    /*** End of Processor Expert internal initialization.                    ***/

    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
    MCU_Freq = delay_init();                                         // 初始化delay函数
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);  // 初始化IO

    lpuartInit();  // 初始化串
    timInit();     // 初始化定时器
    app_init();    // 初始化ADC
    WDOG_DRV_Init(INST_WATCHDOG1, &watchdog1_Config0);
    canInit();
    u1_printf("初始化完毕,MCU运行频率为 %d Mhz \r\n", MCU_Freq);

    while (1) {
        timerFlagOpt();
        if (TKS_10MSF) {
            keyScan();  // 扫描按键
            if (keyTrg0) {
                adcVolt1 = adc0_ch2_read();  // 读取ADC
                u1_printf("adc0_ch1 = %f\r\n", adcVolt1);
                u1_printf("key1 pressed\r\n");
                LED1Toggle();
            }
            if (keyTrg1) {
                adcVolt2 = adc0_ch3_read();  // 读取ADC
                u1_printf("adc0_ch2 = %f\r\n", adcVolt2);
                u1_printf("key2 pressed\r\n");
                LED2Toggle();
            }
            if (keyTrg2) {
                u1_printf("key3 pressed\r\n");
                LED3Toggle();
            }
        }

        if (TKS_500MSF) {
            static uint8_t flag = 0;
            if (flag == 1) {
                flag = 0;
            }
            else {
                flag = 1;
            }
            LED4(flag);
        }
        if (TKS_100MSF) {
            WDOG_DRV_Trigger(INST_WATCHDOG1);
        }

        if (triger) {
            triger = 0;
            u1_printf("接收到的数据:%s", rxdata);
            memset(rxdata, 0, sizeof(rxdata));
        }

        canTask();  // CAN接收数据处理
        delay_us(1);
    }
/*** Don't write any code pass th5is line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
    PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
    /*** End of RTOS startup code.  ***/
    /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
    for (;;) {
        if (exit_code != 0) {
            break;
        }
    }
    return exit_code;
    /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.1 [05.21]
**     for the NXP S32K series of microcontrollers.
**
** ###################################################################
*/
